<?php
namespace SDXApi\Controller;
use Com\Wechat;
use Think\Controller;

class WechatNotifyController extends Controller {
    protected $wechat_object;
    protected $options = array();
	public function __construct(){
        $wechat = M('wechat')->where('default_wx=1')->find();
        $this->options['token']= $wechat['token'];
        $this->options['appid']= $wechat['appid'];
        $this->options['appsecret']= $wechat['appsecret'];
        $this->wechat_object = new Wechat($this->options);
        $this->wechat_object->valid();
	}

	public function index(){
        $data = $this->wechat_object->getRev()->getRevData();
        write_log(json_encode($data),"wechat_notify");
        $type = $this->wechat_object->getRev()->getRevType();
        write_log($type,"wechat_notify");
        switch($type) {
            case Wechat::MSGTYPE_TEXT:
            break;
            case Wechat::MSGTYPE_EVENT:
                if($data['Event']=="subscribe"){
                    $this->subscribe($data);
                    write_log('subscribe',"wechat_notify");
                }
                if($data['Event']=="unsubscribe"){
                    $this->un_subscribe($data);
                    write_log('un_subscribe',"wechat_notify");
                }
            break;
            case Wechat::MSGTYPE_IMAGE:
            break;
        }
    }

    /**
     * 关注事件处理
     * @param $data
     */
    private function subscribe($data){
        $open_id = $data['FromUserName'];
        $user_info = $this->wechat_object->getUserInfo($open_id);
        if(!empty($user_info)){
            if(!empty($user_info['unionid'])){
                $map = array('unionid'=>$user_info['unionid']);
            }else{
                $map = array('openid'=>$user_info['openid']);
            }
            $data = M('wechat_user')->where($map)->find();

            $save_data = $user_info;
            $save_data['headimgurl'] = str_replace("http://","https://",$user_info['headimgurl']);
            $save_data['group_id'] = $user_info['groupid'];
            unset($user_info['groupid']);

            if(!empty($data)){
                M('wechat_user')->where($map)->save($save_data);
            }else{
                $res = $this->register();
                if($res){
                    $save_data['ect_uid'] = $res;
                    M('wechat_user')->add($save_data);   //将数据写入微信用户表
                }
            }
        }
    }

    /**
     * 取消关注事件处理
     * @param $data
     */
    private function un_subscribe($data){
        $open_id = $data['FromUserName'];
        $user_info = $this->wechat_object->getUserInfo($open_id);
        if(!empty($user_info)){
            if(!empty($user_info['unionid'])){
                $map = array('unionid'=>$user_info['unionid']);
            }else{
                $map = array('openid'=>$user_info['openid']);
            }
            $data = M('wechat_user')->where($map)->find();
            $save_data = $user_info;
            if(!empty($data)){
                M('wechat_user')->where($map)->save($save_data);
            }
        }
    }

    /**
     * 注册新用户
     * @return mixed
     */
    private function register(){
        $username = 'wx_' . gmtime().mt_rand(1, 99);
        $password = 'shidexian';

        $res = D('Users')->register($username,$password,$username."@shidexian.com",null);

        return $res;
    }
	
}